package com.hw;

import com.hw.entity.News;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import java.util.Date;

/**
 * Hello world!
 */
public class App {
    public static void main(String[] args) {
        // 1.创建SessionFactory
        Configuration configuration = new Configuration().configure();

        SessionFactory sessionFactory = configuration.buildSessionFactory();

// 2. 获得一个 Session 对象
        Session session = sessionFactory.openSession();

// 3. 开启事务
        Transaction transaction = session.beginTransaction();

// 4. 执行保存操作
        News news = new News( "标题", "内容" );
        session.save( news );

// 5. 提交事务
        transaction.commit();

// 6. 关闭 Session
        session.close();

// 7. 关闭 SessionFactory 对象
        sessionFactory.close();
        System.out.println( news );
    }

}
